<?php
/**
 * Widget Service
 *
 * @package  Addons
 * @author   Wing <wxywing@gmail.com>
 * @version  $Id: WidgetService.class.php Mon Apr 23 06:45:00 GMT 2012 wing $ 
 */
class WidgetService extends Model {
    protected	$tableName	=	'user_widgets';	
	protected	$fields		=	array (0 => 'id',1 => 'uid',2 => 'role_id',3 => 'widget_id',4 => 'status',5 => 'updated', '_autoinc' => true,'_pk' => 'id');
	
	
	public function registerWidget($uid, $role_id, $widget_id, $status = 1)
	{
		$_userWidget = array();
		
		$_userWidget['uid'] = $uid;
		$_userWidget['role_id'] = $role_id;
		$_userWidget['widget_id'] = $widget_id;
		$_userWidget['status'] = $status;
		$_userWidget['updated'] = time();
		return $this->add($_userWidget);
	}

	public function getUserWidgetList($uid, $role_id)
	{
		$map['uid'] = $uid;
		$map['role_id']  = $role_id;
		return $this->where($map)->find();
	}
	
	public function getUserWidgetStatus($uid, $role_id, $widget_id, $status = 1)
	{
		$map['uid'] = $uid;
		$map['role_id']  = $role_id;
		$map['widget_id']  = $widget_id;
		$map['status']  = $status;
		return $this->where($map)->find();
	}
	
	public function getWidgetInfo($widget_name)
	{
		$result = array();
		$uid = $_SESSION['mid'];
		$role_id = $_SESSION['role'];
		if (empty($uid)) return ;
		
		$widgetInfo = model('Widget')->getWidgetInfoByName($widget_name);
		/*$widget_id = $widgetInfo['id'];
		$isEnabled = $this->getUserWidgetStatus($uid, $role_id, $widget_id);
		if ($isEnabled){
			$result =  $widgetInfo;
		} */
		
		return $widgetInfo;
	}
}